import type { ActionButton } from '@/components/table/src/types/action-button';
import type { BasicCrudProps } from '@/components/crud/src/props';

const key = Symbol('basic-crud-context');

export interface CrudContext {
  /**
   * 参数
   */
  props: BasicCrudProps;
  /**
   * 工具栏按钮
   */
  toolbarActionButtons?:
    | ActionButton[]
    | ((defaultButtons: {
      createButton: ActionButton;
      batchDeleteButton: ActionButton;
    }) => ActionButton[]);
  /**
   * 数据操作按钮
   */
  dataActionButtons?:
    | ActionButton[]
    | ((defaultButtons: {
      modifyButton: ActionButton;
      deleteButton: ActionButton;
    }) => ActionButton[]);
  /**
   * 新增
   */
  handleCreate: () => void;
  /**
   * 编辑
   */
  handleModify: (row: Recordable) => void;
  /**
   * 删除
   */
  handleDelete: (row: Recordable) => void;
  /**
   * 批量删除
   */
  handleBatchDelete: () => void;
}

export function createCrudContext(context: CrudContext) {
  provide(key, context);
}

export function useCrudContext() {
  return inject<CrudContext>(key) as CrudContext;
}
